Imports System.Data

Partial Class Admin_Module_Access
    Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.EditType)
        sec.SetPageAccess(Session("UserName"))
        sec = Nothing
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then


            If department_cmb.Items.Count < 1 Then
                Dim zClass As New LookUp.General_Lookup
                department_cmb.Items.Clear()
                zClass.Loader(department_cmb, "DEPARTMENT", "Desc", "ID")
                department_cmb.Items.Insert(0, "")

                If Session("dept_cd") <> Nothing And Session("role_cd") <> Nothing Then
                    department_cmb.Items.FindByValue(Session("dept_cd")).Selected = True
                    department_cmb.Enabled = False

                    role_cmb.Items.Clear()
                    zClass.Loader(role_cmb, "ROLE", "role_desc", "role_cd", department_cmb.SelectedValue)
                    role_cmb.Items.Insert(0, "")

                    role_cmb.Items.FindByValue(Session("role_cd")).Selected = True
                    role_cmb.Enabled = False

                    zClass.LookUpCode = "MODULE_ACCESS"
                    DataGrid.CurrentPageIndex = 0

                    zClass.DataGridFill(DataGrid, Label1, role_cmb.SelectedValue)
                    Call BindRpt()
                Else
                    Back_btn.Visible = False
                End If

            End If

            Dim Edit_Image As Image = CType(Me.Master.FindControl("U3"), Image)
            Edit_Image.ImageUrl = "~/images/redT.PNG"
            Dim Edit_Button As LinkButton = CType(Me.Master.FindControl("Module_Access_btn"), LinkButton)
            Edit_Button.ForeColor = Drawing.Color.Black
            Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Users_Panel"), Panel)
            Edit_Panel.Visible = True
        End If

        SecurePage()
    End Sub

    Protected Sub department_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles department_cmb.SelectedIndexChanged
        If department_cmb.SelectedItem.Text = "" Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        role_cmb.Items.Clear()
        zClass.Loader(role_cmb, "ROLE", "role_desc", "role_cd", department_cmb.SelectedValue)
        role_cmb.Items.Insert(0, "")
    End Sub

    Protected Sub role_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles role_cmb.SelectedIndexChanged
        If role_cmb.SelectedItem.Text = "" Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "MODULE_ACCESS"
        DataGrid.CurrentPageIndex = 0
        DataGrid.EditItemIndex = -1
        zClass.DataGridFill(DataGrid, Label1, role_cmb.SelectedValue)
        Call BindRpt()

    End Sub

    Private Sub BindRpt()
        Dim dao As New DAOMain.CommonDAO

        dao.AddParameter(role_cmb.SelectedValue)
        Dim dt As DataTable = dao.ExecQryProc("sp_get_report_access")

        Report_dg.DataSource = dt
        Report_dg.DataBind()

        dao = Nothing
    End Sub

    Protected Sub All_chkbox_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If sender.id <> "Access_chkbox" Then
            Dim chkALL As CheckBox = CType(DataGrid.Items(DataGrid.EditItemIndex).Cells(2).Controls(1), CheckBox)
            Dim chkVIEW As CheckBox = CType(DataGrid.Items(DataGrid.EditItemIndex).Cells(3).Controls(1), CheckBox)
            Dim chkADD As CheckBox = CType(DataGrid.Items(DataGrid.EditItemIndex).Cells(4).Controls(1), CheckBox)
            Dim chkEDIT As CheckBox = CType(DataGrid.Items(DataGrid.EditItemIndex).Cells(5).Controls(1), CheckBox)
            Dim chkDELETE As CheckBox = CType(DataGrid.Items(DataGrid.EditItemIndex).Cells(6).Controls(1), CheckBox)
            Dim chkPRINT As CheckBox = CType(DataGrid.Items(DataGrid.EditItemIndex).Cells(7).Controls(1), CheckBox)

            If chkVIEW.Visible = True Then chkVIEW.Checked = chkALL.Checked
            If chkADD.Visible = True Then chkADD.Checked = chkALL.Checked
            If chkEDIT.Visible = True Then chkEDIT.Checked = chkALL.Checked
            If chkDELETE.Visible = True Then chkDELETE.Checked = chkALL.Checked
            If chkPRINT.Visible = True Then chkPRINT.Checked = chkALL.Checked
        End If
    End Sub

    Protected Sub Back_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Back_btn.Click
        Session.Remove("role_cd")
        Session.Remove("dept_cd")
        Response.Redirect("~/Admin/User_Roles.aspx")

    End Sub

    Protected Sub DataGrid_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.CancelCommand
        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "MODULE_ACCESS"
        DataGrid.CurrentPageIndex = 0
        DataGrid.EditItemIndex = -1
        zClass.DataGridFill(DataGrid, Label1, role_cmb.SelectedValue)
        ViewState.Remove("module_access_cd")
    End Sub

    Protected Sub DataGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.EditCommand
        DataGrid.EditItemIndex = e.Item.ItemIndex
        ViewState("module_access_cd") = e.Item.Cells(0).Text
        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "MODULE_ACCESS"
        zClass.DataGridFill(DataGrid, Label1, role_cmb.SelectedValue)

    End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "MODULE_ACCESS"
        DataGrid.CurrentPageIndex = e.NewPageIndex

        zClass.DataGridFill(DataGrid, Label1, role_cmb.SelectedValue)

    End Sub

    Protected Sub DataGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.UpdateCommand
        Dim sConn As New Data.SqlClient.SqlConnection
        Dim sCom As New Data.SqlClient.SqlCommand

        Try

            Dim chkVIEW As CheckBox = CType(e.Item.Cells(3).Controls(1), CheckBox)
            Dim chkADD As CheckBox = CType(e.Item.Cells(4).Controls(1), CheckBox)
            Dim chkEDIT As CheckBox = CType(e.Item.Cells(5).Controls(1), CheckBox)
            Dim chkDELETE As CheckBox = CType(e.Item.Cells(6).Controls(1), CheckBox)
            Dim chkPRINT As CheckBox = CType(e.Item.Cells(7).Controls(1), CheckBox)

            Dim iVIEW, iADD, iEDIT, iDELETE, iPRINT As Integer

            If chkVIEW.Visible = True Then iVIEW = CInt(chkVIEW.Checked)
            If chkADD.Visible = True Then iADD = CInt(chkADD.Checked)
            If chkEDIT.Visible = True Then iEDIT = CInt(chkEDIT.Checked)
            If chkDELETE.Visible = True Then iDELETE = CInt(chkDELETE.Checked)
            If chkPRINT.Visible = True Then iPRINT = CInt(chkPRINT.Checked)

            If (iADD = 1) Or (iEDIT = 1) Or (iDELETE = 1) Or (iPRINT = 1) Then iVIEW = 1

            If iVIEW = 0 Then iADD = 0 : iEDIT = 0 : iDELETE = 0 : iPRINT = 0

            'If ViewState("data") = "Search" Then iVIEW = 1  'default page must have view rights

            'Update record

            sConn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("CnnStr")
            sCom.CommandType = Data.CommandType.Text
            sCom.Connection = sConn

            sCom.CommandText = "UPDATE role_dtl SET view_tag=" & iVIEW & ", add_tag=" & iADD & _
              ", edit_tag=" & iEDIT & ", delete_tag=" & iDELETE & ", print_tag=" & iPRINT & _
              ", role_dtl_last_upd_by='" & Session("UserName") & _
              "', role_dtl_last_upd_dt='" & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & _
              "' WHERE role_cd='" & Trim(role_cmb.SelectedValue) & _
              "' AND module_access_cd='" & ViewState("module_access_cd") & "'"

            sConn.Open()
            sCom.ExecuteNonQuery()

            Dim zClass As New LookUp.General_Lookup
            zClass.LookUpCode = "MODULE_ACCESS"
            DataGrid.CurrentPageIndex = 0
            DataGrid.EditItemIndex = -1
            zClass.DataGridFill(DataGrid, Label1, role_cmb.SelectedValue)
            ViewState.Remove("module_access_cd")
        Catch ex As Exception

        Finally
            sConn.Close()
        End Try

    End Sub

    Protected Sub UpdateRpt_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateRpt_btn.Click
        Dim daoDel As New DAOMain.CommonDAO

        daoDel.AddParameter(role_cmb.SelectedValue)
        daoDel.ExecQryProc("sp_report_access_mstr_del")

        daoDel = Nothing

        Dim dgi As DataGridItem

        For Each dgi In Report_dg.Items
            Select Case dgi.ItemType
                Case ListItemType.Item, ListItemType.AlternatingItem
                    Dim ch As CheckBox = dgi.FindControl("Access_chkbox")

                    If Not IsNothing(ch) Then
                        If ch.Checked Then
                            Dim daoIns As New DAOMain.CommonDAO
                            Dim cf As New CIMSfunc

                            With daoIns
                                .AddParameter(cf.GetLastID("report_access_cd", "report_access_mstr"))
                                .AddParameter(dgi.Cells(0).Text)
                                .AddParameter(role_cmb.SelectedValue)
                                .AddParameter(ch.Checked)

                                Dim pf As New PolyFunc.ASPFunc

                                If .ExecCmdProc("sp_report_access_mstr_ins") Then
                                    pf.MessageAlert(Me, "Report Access Updated")
                                Else
                                    pf.MessageAlert(Me, "Report Access Update Failed")
                                End If

                                pf = Nothing
                            End With

                            daoIns = Nothing
                        End If
                    End If
            End Select
        Next

        Call BindRpt()
    End Sub
End Class